package com.guigu.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.dispatch.entity.CacheLine;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dispatch.entity.CacheLineUse;
import com.guigu.dispatch.entity.OrderClassifyAttach;
import com.guigu.dispatch.entity.OrderClassifyOrder;
import com.guigu.dispatch.mapper.CacheLineMapper;
import com.guigu.dispatch.service.CacheLineService;
import com.guigu.dispatch.service.CacheLineUseService;
import com.guigu.dispatch.service.OrderClassifyAttachService;
import com.guigu.dispatch.service.OrderClassifyOrderService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.ResultCode;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 缓冲路线表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-18
 */
@Service
@Transactional
public class CacheLineServiceImpl extends ServiceImpl<CacheLineMapper, CacheLine> implements CacheLineService {
    private OrderClassifyOrderService orderClassifyOrderService;
    private CacheLineUseService cacheLineUseService;
    private OrderClassifyAttachService orderClassifyAttachService;

    public CacheLineServiceImpl(OrderClassifyOrderService orderClassifyOrderService, CacheLineUseService cacheLineUseService, OrderClassifyAttachService orderClassifyAttachService) {
        this.orderClassifyOrderService = orderClassifyOrderService;
        this.cacheLineUseService = cacheLineUseService;
        this.orderClassifyAttachService = orderClassifyAttachService;
    }

    /**
     * 根据订单Id获取缓存线路表
     *
     * @param orderId 订单Id
     * @return
     */
    @Override
    public CacheLineUse findCacheLineById(String orderId) {
        QueryWrapper<OrderClassifyOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        OrderClassifyOrder orderClassifyOrder = orderClassifyOrderService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(orderClassifyOrder)) {
            throw new TmsException(ResultCode.ERROR_CODE, "对象orderClassifyOrder为空!!");
        }

        QueryWrapper<CacheLineUse> q = new QueryWrapper<>();
        q.eq("order_classify_id", orderClassifyOrder.getOrderClassifyId());
        CacheLineUse use = cacheLineUseService.getOne(q);
        return use;
    }

    /**
     * 根据订单Id获取车次车辆司机Id
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderClassifyAttach findOrderClassifyAttachByOrderId(String orderId) {
        QueryWrapper<OrderClassifyOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        OrderClassifyOrder orderClassifyOrder = orderClassifyOrderService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(orderClassifyOrder)) {
            throw new TmsException(ResultCode.ERROR_CODE, "对象orderClassifyOrder为空!!");
        }
        /**
         * 根据订单分类Id获取车辆车次司机
         */

        QueryWrapper<OrderClassifyAttach> q = new QueryWrapper<>();
        q.eq("order_classify_id", orderClassifyOrder.getOrderClassifyId());
        OrderClassifyAttach attach = orderClassifyAttachService.getOne(q);
        return attach;
    }
}
